import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { defaultRouter } from './modules/default'
import { setDocumentTitle } from '@/utils'
// import { useAppStore } from '@/store/app'

// const appStore = useAppStore()
const routes = [...defaultRouter]

const router = createRouter({
	history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
	routes,
	strict: true,
	scrollBehavior: () => ({ left: 0, top: 0 })
})

// 路由守卫
router.beforeEach((to, from, next) => {
	// 获取token，这里假设token存储在localStorage中
	const token = localStorage.getItem('token')

	// 如果是登录页，直接放行
	if (to.path === '/login') {
		next()
		return
	}

	// 如果没有token且不是登录页，重定向到登录页
	if (!token) {
		next('/login')
		return
	}

	setDocumentTitle(to.meta.title as string)

	// 其他情况正常放行
	next()
})

export default router
